package com.technodo.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.media.MediaPlayer;
import android.os.Environment;
import android.util.Log;

public class GlobalPreferences {
	public static final String TAG = "GlobalPreferences";

	public static MediaPlayer mp;
	private long 	gamesPlayed = 0;	
	private String dataDirPath;
	private final String defaultRules = "/data/rules/cs.json";
	private String defaultTechs = "/data/compsci/CsTech.json";
	private String defaultProducts = "/data/compsci/CsProducts.json";
	private String defaultDirectors = "/data/compsci/CsDirectors.json";

	// FIXME : Hardcode path for default icons
	public final static String defaultTechIcon = null;
	public final static String defaultProductIcon = null;
	public final static String defaultDirectorIcon = null;


	/**
	 *  Shared Preferences Handle (Read only)
	 */
	SharedPreferences 	technodoPrefs;

	/**
	 * Shared Preferences editor
	 */
	private Editor 		technodoPrefsEditor;

	// Shared Preferences in Android
	private Boolean		soundon;		// Whether sound is on or off		

	/**
	 * Path of Techcards
	 */
	private String[]			techCards;
	private String[]			directorCards;
	private String[]			productCards;
	private String[]			profiles;

	/**
	 *  Save and Retrieve Packages from the system
	 */
	private String 				packagePathBase;

	/**
	 * Ref to make it a singleton class and pass reference around
	 */
	private static GlobalPreferences ref; 

	// Noone can call the constructor excepting us
	private GlobalPreferences() {
	}

	/**
	 * If Global Preferences is not created, creates a global preferences class
	 * @return
	 */
	public static GlobalPreferences getGlobalPreferences() {
		if (ref == null) {
			// its ok to call the constructor now
			ref = new GlobalPreferences();
		}
		return ref;
	}

	public Object clone() throws CloneNotSupportedException {
		throw new CloneNotSupportedException();
		// that'll teach 'em
		// cannot clone this class
	}

	// Getter & Setter methods for variables
	public void setDataDir(String dataDirPath) {
		this.dataDirPath = dataDirPath;
	}

	public String getDataDir() {
		return dataDirPath;
	}

	public String getDefaultRules() {
		return defaultRules;
	}

	public long getGamesPlayed() {
		gamesPlayed = technodoPrefs.getLong(StringConstants.PREF_LONG_GAMESPLAYED, 0);
		return gamesPlayed;
	}

	public void incrementGamesPlayed() {
		gamesPlayed = technodoPrefs.getLong(StringConstants.PREF_LONG_GAMESPLAYED, 0);
		gamesPlayed = gamesPlayed + 1;
		technodoPrefsEditor.putLong(StringConstants.PREF_LONG_GAMESPLAYED, gamesPlayed);
		technodoPrefsEditor.commit();
	}

	public SharedPreferences gettechnodoPrefs() {
		return technodoPrefs;
	}

	public void settechnodoPrefs(SharedPreferences technodoPrefs) {
		technodoPrefsEditor = technodoPrefs.edit();
		this.technodoPrefs = technodoPrefs;
	}

	public Boolean getSoundon() {
		soundon = technodoPrefs.getBoolean(StringConstants.PREF_BOOL_SOUNDON, true);
		return soundon;
	}

	public void setSoundon(Boolean soundon) {
		technodoPrefsEditor.putBoolean(StringConstants.PREF_BOOL_SOUNDON, soundon);
		technodoPrefsEditor.commit();
		this.soundon = soundon;
	}

	//To play the sound
	public static void playSound(Context context, int id){
			 mp = MediaPlayer.create(context, id);
			if(id != 0){
				mp.start();
			}
			else {
				Log.d(TAG, "Wrong id");
			}
	}
	
	//To stop the sound
	public static void stopSound(){
		mp.stop();
	}
	
	private String[] assimilateArray(String basename) {
		int num = technodoPrefs.getInt("num"+basename, 0);
		String[] collector = new String[num];
		for(int i=0; i<num; i++) {
			collector[i] = technodoPrefs.getString(basename+i, null);
		}

		Log.d(TAG, "Assimilated array to preferences" + basename + " number " + num);

		return collector;
	}

	private void appendElement(String basename, String element) {
		String[] currentList = assimilateArray(basename);
		for(int i=0; i< currentList.length; i++) {
			// Duplicate Check, remove Duplicate Elements from the list
			if(currentList[i].matches(element))
				return;
		}

		int num = currentList.length;
		technodoPrefsEditor.putString(basename+num, element);
		num=num+1;
		technodoPrefsEditor.putInt("num"+basename, num);
		technodoPrefsEditor.commit();
	}

	private void clearPreference(String basename) {
		int num = technodoPrefs.getInt("num"+basename, 0);
		for(int i=0; i<num; i++) {
			technodoPrefsEditor.remove(basename+num);
		}
		technodoPrefsEditor.putInt("num"+basename, 0);
	}

	public void clearAllPreferences() {
		clearPreference(StringConstants.PREFS_DIRECTOR_PATH);
		clearPreference(StringConstants.PREFS_PRODUCT_PATH);
		clearPreference(StringConstants.PREFS_TECH_PATH);
		clearPreference(StringConstants.PREF_PROFILE);
		technodoPrefsEditor.putLong(StringConstants.PREF_LONG_GAMESPLAYED, 0);
		technodoPrefsEditor.commit();
		Log.d(TAG, "Cleared all preferences");
	}


	public int getDifficulty() {
		return technodoPrefs.getInt(StringConstants.PREFS_INT_DIFFICULTY, 0);
	}

	public void setDifficulty(int difficulty) {
		technodoPrefsEditor.putInt(StringConstants.PREFS_INT_DIFFICULTY, difficulty);
		technodoPrefsEditor.commit();
	}


	public boolean fileAddToPreferences(String filename) {
		if(filename.endsWith("Tech.json")) {
			setTechCards(filename);
			Log.d(TAG, "Classified file : Tech : " + filename);
			return true;			
		} else if(filename.endsWith("Products.json")) {
			setProductCards(filename);
			Log.d(TAG, "Classified file : Products : " + filename);
			return true;			
		} else if(filename.endsWith("Directors.json")) {
			setDirectorCards(filename);
			Log.d(TAG, "Classified file : Director : " + filename);
			return true;			
		} else if(filename.contains("Profile")) {
			setProfiles(filename);
			Log.d(TAG, "Classified file : Profile : " + filename);
			return true;
		} else {
			Log.d(TAG, "Unclassified file : " + filename);
			return false;
		}
	}


	public void setPackagePathBase(String packagePathBase) {
		this.packagePathBase = packagePathBase;
	}

	public String getPackagePathBase() {
		packagePathBase = Environment.getExternalStorageDirectory() + StringConstants.FOLDER_SDCARD;
		if(Environment.getExternalStorageState().compareTo(Environment.MEDIA_MOUNTED) == 0)
			return packagePathBase;
		return null;
	}

	public String[] getTechCards() {
		techCards = assimilateArray(StringConstants.PREFS_TECH_PATH);
		return techCards;
	}

	public void setTechCards(String item) {
		appendElement(StringConstants.PREFS_TECH_PATH, item);
	}

	public String[] getDirectorCards() {
		directorCards = assimilateArray(StringConstants.PREFS_DIRECTOR_PATH);
		return directorCards;
	}

	public void setDirectorCards(String item) {
		appendElement(StringConstants.PREFS_DIRECTOR_PATH, item);;
	}

	public String[] getProductCards() {
		productCards = assimilateArray(StringConstants.PREFS_PRODUCT_PATH);
		return productCards;
	}

	public void setProductCards(String item) {
		appendElement(StringConstants.PREFS_PRODUCT_PATH, item);
	}

	public String[] getProfiles() {
		profiles = assimilateArray(StringConstants.PREF_PROFILE);
		Log.d(TAG, "getProfiles got " + profiles.length + " profiles : " + profiles.toString());
		return profiles;
	}

	public void setProfiles(String item) {
		appendElement(StringConstants.PREF_PROFILE, item);
	}


	public String getDefaultTechIcon() {
		return defaultTechIcon;
	}

	public String getDefaultProductIcon() {
		return defaultProductIcon;
	}

	public String getDefaultDirectorIcon() {
		return defaultDirectorIcon;
	}

	public String getDefaultDirectors() {
		return defaultDirectors;
	}

	public void setDefaultDirectors(String defaultDirectors) {
		this.defaultDirectors = defaultDirectors;
	}

	public String getDefaultProducts() {
		return defaultProducts;
	}

	public void setDefaultProducts(String defaultProducts) {
		this.defaultProducts = defaultProducts;
	}

	public String getDefaultTechs() {
		return defaultTechs;
	}

	public void setDefaultTechs(String defaultTechs) {
		this.defaultTechs = defaultTechs;
	}	

}
